import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**角色属性枚举*/
export const enum E_BLActor {
	/**是否暴击*/
	IS_CRIT = 0,
	/**枚举数量*/
	EnumCount = 1
}


/**角色*/
export class BLActor {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_BL_ACTOR_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_BL_ACTOR_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: BLActor[] = [];

	/**创建入口*/
	public static create(json?: string): BLActor {
		const c = this.POOL.pop() || new BLActor();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**是否暴击*/
	protected _isCrit: boolean;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (BLActor.POOL.indexOf(this) === -1) {
			BLActor.POOL.push(this);
		}
	}

	/**是否暴击*/
	public get isCrit() {
		return this._isCrit;
	}


	/**是否暴击*/
	public set isCrit(v: boolean) {
		this._isCrit = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_BLActor): any {
		switch (k) {
			case E_BLActor.IS_CRIT: return this._isCrit;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_BLActor, v: any): void {
		switch (k) {
			case E_BLActor.IS_CRIT: this._isCrit = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//是否暴击
		this._isCrit = false;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._isCrit) {
			json.isCrit = this._isCrit;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.isCrit) {
			this._isCrit = json.isCrit;
		}
		return this;
	}

}
